package com.woniuxy.background.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.advice.SysLog;
import com.woniuxy.background.common.RestResult;
import com.woniuxy.background.pojo.User;
import com.woniuxy.background.service.UsersService;
import com.woniuxy.background.vo.UserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;  //alt+enter

    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("user:user:list")
    public RestResult list(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize,
                           UserVO vo){
        List<User> users = usersService.findUserByPage(currentPage,pageSize,vo);
        PageInfo pageInfo = new PageInfo(users);

        return RestResult.success(pageInfo);
    }


    @DeleteMapping("/deleteUser/{keys}")
    @ResponseBody
    @SysLog(value = "删除多个用户")
    @RequiresPermissions("user:user:delete")
    public RestResult deleteUser(@PathVariable("keys") String keys){
        String[] arr = keys.split(",");
        List<Integer> list = new ArrayList();
        for (String id : arr) {
            list.add(Integer.parseInt(id));
        }
        try {
            usersService.batchDeleteUser(list);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("删除失败");
        }
        return RestResult.success("删除成功");
    }


    @PostMapping("/saveUser")
    @ResponseBody
    @SysLog(value = "添加用户")
    @RequiresPermissions("user:user:save")
    public RestResult saveUser(User users){
       /* User user = usersService.findUserByEmail(users.getEmail());
        if(user != null){
            return RestResult.failed("用户邮箱已经存在");
        }*/
        try {
            users.setCreated(new Date());
            usersService.saveUser(users);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("添加失败");
        }
        return RestResult.success("添加成功");
    }


    @GetMapping("/userEdit/{uid}")
    public ModelAndView userEdit(@PathVariable("uid")int uid, ModelAndView modelAndView){
        User user = usersService.findById(uid);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("users/editUser");
        return modelAndView;
    }

    @DeleteMapping("/delete/{uid}")
    @ResponseBody
    @SysLog(value = "删除用户")
    @RequiresPermissions("user:user:delete")
    public RestResult deleteUser(@PathVariable("uid")int uid){
        try {
            usersService.deleteByPrimaryKey(uid);
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.failed("删除失败！");
        }
        return RestResult.success("删除成功！");
    }

    @PostMapping("/updUser")
    @ResponseBody
    @SysLog(value = "更新用户")
    @RequiresPermissions("user:user:update")
    public RestResult updUser(User user){
        try {
            usersService.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            e.printStackTrace();
            RestResult.failed("更新失败！");
        }
        return RestResult.success("更新成功！");
    }


}
